<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>synchronized 和 lock 性能比较 | flyingzc's blog</title><meta name="author" content="flyingzc"><meta name="copyright" content="flyingzc"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="synchronized 与 Lock 性能比较JDK1.812345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899">
<meta property="og:type" content="article">
<meta property="og:title" content="synchronized 和 lock 性能比较">
<meta property="og:url" content="https://flyingzc.github.io/articles/concurrent/concurrent10-sync-vs-lock.html">
<meta property="og:site_name" content="flyingzc&#39;s blog">
<meta property="og:description" content="synchronized 与 Lock 性能比较JDK1.812345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://flyingzc.github.io/images/zc-icon.jpeg">
<meta property="article:published_time" content="2019-12-02T16:00:00.000Z">
<meta property="article:modified_time" content="2025-01-21T01:51:41.210Z">
<meta property="article:author" content="flyingzc">
<meta property="article:tag" content="Concurrent">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://flyingzc.github.io/images/zc-icon.jpeg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "synchronized 和 lock 性能比较",
  "url": "https://flyingzc.github.io/articles/concurrent/concurrent10-sync-vs-lock.html",
  "image": "https://flyingzc.github.io/images/zc-icon.jpeg",
  "datePublished": "2019-12-02T16:00:00.000Z",
  "dateModified": "2025-01-21T01:51:41.210Z",
  "author": [
    {
      "@type": "Person",
      "name": "flyingzc",
      "url": "https://flyingzc.github.io/"
    }
  ]
}</script><link rel="shortcut icon" href="/images/zc-icon.jpeg"><link rel="canonical" href="https://flyingzc.github.io/articles/concurrent/concurrent10-sync-vs-lock.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="DentpX3LnP_WNgEeFnNIcTZZky-Bby-zL-uI_9GqTAw"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'synchronized 和 lock 性能比较',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/self/github-dark.css"><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">flyingzc's blog</span></a><a class="nav-page-title" href="/"><span class="site-name">synchronized 和 lock 性能比较</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">synchronized 和 lock 性能比较</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-12-02T16:00:00.000Z" title="发表于 2019-12-03 00:00:00">2019-12-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-01-21T01:51:41.210Z" title="更新于 2025-01-21 09:51:41">2025-01-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Concurrent/">Concurrent</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="synchronized-与-Lock-性能比较"><a href="#synchronized-与-Lock-性能比较" class="headerlink" title="synchronized 与 Lock 性能比较"></a>synchronized 与 Lock 性能比较</h1><h2 id="JDK1-8"><a href="#JDK1-8" class="headerlink" title="JDK1.8"></a>JDK1.8</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">D:\soft\Java\jdk1.8.0_172\bin\java.exe &quot;-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=57167:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin&quot; -Dfile.encoding=UTF-8 -classpath &quot;D:\soft\Java\jdk1.8.0_172\jre\lib\charsets.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\deploy.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\javaws.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jce.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jfr.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jsse.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\management-agent.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\plugin.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\resources.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\myworkspace\03mygithub\MyThread\bin;D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\junit-4.12.jar;D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\hamcrest-core-1.3.jar&quot; com.zc.concurrent.issue.ReentrantLockVsSynchronizedTest<br>-------------------------------------<br>// 线程数1,每个线程循环 100000 次<br>3789172(elapse), testLongAdder: result=100000, threadCount=1, loopCount=100000<br>6642633(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000<br>6841152(elapse), testReentrantLockFair: result=100000, threadCount=1, loopCount=100000<br>8382098(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000<br>44980066(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000<br>-------------------------------------<br>// 线程数2,每个线程循环 100000 次<br>2647611(elapse), testLongAdder: result=300000, threadCount=2, loopCount=100000<br>3772457(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000<br>5013131(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000<br>5831547(elapse), testReentrantLockFair: result=300000, threadCount=2, loopCount=100000<br>9820412(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000<br>-------------------------------------<br>// 线程数4,每个线程循环 100000 次<br>2838213(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000<br>3695923(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000<br>12967987(elapse), testLongAdder: result=700000, threadCount=4, loopCount=100000<br>13843291(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000<br>1132170308(elapse), testReentrantLockFair: result=700000, threadCount=4, loopCount=100000<br>-------------------------------------<br>// 线程数6,每个线程循环 100000 次<br>2340008(elapse), testLongAdder: result=1300000, threadCount=6, loopCount=100000<br>5598133(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000<br>11595358(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000<br>17305800(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000<br>1706668497(elapse), testReentrantLockFair: result=1300000, threadCount=6, loopCount=100000<br>-------------------------------------<br>// 线程数8,每个线程循环 100000 次<br>2604212(elapse), testLongAdder: result=2100000, threadCount=8, loopCount=100000<br>7267222(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000<br>15585101(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000<br>20813467(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000<br>2392860159(elapse), testReentrantLockFair: result=2100000, threadCount=8, loopCount=100000<br>-------------------------------------<br>// 线程数10,每个线程循环 100000 次<br>6970763(elapse), testLongAdder: result=3100000, threadCount=10, loopCount=100000<br>9930961(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000<br>20397367(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000<br>25850936(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000<br>2794116491(elapse), testReentrantLockFair: result=3100000, threadCount=10, loopCount=100000<br>-------------------------------------<br>// 线程数50,每个线程循环 100000 次<br>14844099(elapse), testLongAdder: result=8100000, threadCount=50, loopCount=100000<br>47918859(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000<br>98661562(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000<br>126939598(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000<br>14410074177(elapse), testReentrantLockFair: result=8100000, threadCount=50, loopCount=100000<br>-------------------------------------<br>// 线程数100,每个线程循环 100000 次<br>29294384(elapse), testLongAdder: result=18100000, threadCount=100, loopCount=100000<br>90143110(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000<br>193961488(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000<br>244101889(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000<br>31166365222(elapse), testReentrantLockFair: result=18100000, threadCount=100, loopCount=100000<br>-------------------------------------<br>// 线程数200,每个线程循环 100000 次<br>69702637(elapse), testLongAdder: result=38100000, threadCount=200, loopCount=100000<br>170973104(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000<br>341206964(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000<br>489087584(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000<br>63709112896(elapse), testReentrantLockFair: result=38100000, threadCount=200, loopCount=100000<br>-------------------------------------<br>// 线程数500,每个线程循环 100000 次<br>133355267(elapse), testLongAdder: result=88100000, threadCount=500, loopCount=100000<br>465237384(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000<br>982221507(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000<br>1246923387(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000<br>163173197284(elapse), testReentrantLockFair: result=88100000, threadCount=500, loopCount=100000<br>-------------------------------------<br>-------------------------------------<br>// 线程数500,每个线程循环 10000 次<br>46909254(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000<br>53434593(elapse), testLongAdder: result=93100000, threadCount=500, loopCount=10000<br>76151149(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000<br>115951527(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000<br>16067416290(elapse), testReentrantLockFair: result=93100000, threadCount=500, loopCount=10000<br>-------------------------------------<br>// 线程数500,每个线程循环 1000 次<br>21785245(elapse), testLongAdder: result=93600000, threadCount=500, loopCount=1000<br>22926219(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000<br>23726455(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000<br>23893305(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000<br>1583798403(elapse), testReentrantLockFair: result=93600000, threadCount=500, loopCount=1000<br>-------------------------------------<br>// 线程数500,每个线程循环 100 次<br>21886704(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100<br>22070561(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100<br>22847925(elapse), testLongAdder: result=93650000, threadCount=500, loopCount=100<br>24225539(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100<br>133848487(elapse), testReentrantLockFair: result=93650000, threadCount=500, loopCount=100<br>-------------------------------------<br>// 线程数500,每个线程循环 10 次<br>20742504(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10<br>21348326(elapse), testLongAdder: result=93655000, threadCount=500, loopCount=10<br>21286160(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10<br>21943591(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10<br>22087863(elapse), testReentrantLockFair: result=93655000, threadCount=500, loopCount=10<br>-------------------------------------<br>// 线程数500,每个线程循环 1 次<br>20936918(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1<br>21259769(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1<br>21328093(elapse), testLongAdder: result=93655500, threadCount=500, loopCount=1<br>22007810(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1<br>23318273(elapse), testReentrantLockFair: result=93655500, threadCount=500, loopCount=1<br>-------------------------------------<br>// 线程数1000,每个线程循环 1 次<br>53460398(elapse), testLongAdder: result=1500, threadCount=1000, loopCount=1<br>58974079(elapse), testAtomicInteger: result=1500, threadCount=1000, loopCount=1<br>82545706(elapse), testSynchronized: result=1500, threadCount=1000, loopCount=1<br>130621152(elapse), testReentrantLockFair: result=1500, threadCount=1000, loopCount=1<br>200410000(elapse), testReentrantLockUnfair: result=1500, threadCount=1000, loopCount=1<br>-------------------------------------<br>// 线程数1500,每个线程循环 1 次<br>84837038(elapse), testSynchronized: result=3000, threadCount=1500, loopCount=1<br>104347079(elapse), testAtomicInteger: result=3000, threadCount=1500, loopCount=1<br>109697722(elapse), testReentrantLockUnfair: result=3000, threadCount=1500, loopCount=1<br>138776874(elapse), testReentrantLockFair: result=3000, threadCount=1500, loopCount=1<br>212777150(elapse), testLongAdder: result=3000, threadCount=1500, loopCount=1<br>-------------------------------------<br><br>Process finished with exit code 0<br><br></code></pre></td></tr></table></figure>
<p>从上面的测试数据能看出来,在JDK1.8下<code>synchronized 比 ReentrantLock 性能要好很多</code>,从上面数据来看正常能快个3~5倍这样.</p>
<p>ReentrantLock 公平模式性能很差,并发竞争大的情况下,从数据看 正常比 非公平模式能慢两个数量级.</p>
<p>AtomicInteger 和 LongAdder 在这种场景下性能还是比 ReentrantLock 要好一些的.</p>
<h2 id="JDK1-7"><a href="#JDK1-7" class="headerlink" title="JDK1.7"></a>JDK1.7</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">D:\soft\Java\JDK1.7\bin\java.exe &quot;-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=60620:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin&quot; -Dfile.encoding=UTF-8 -classpath D:\soft\Java\JDK1.7\jre\lib\charsets.jar;D:\soft\Java\JDK1.7\jre\lib\deploy.jar;D:\soft\Java\JDK1.7\jre\lib\ext\access-bridge-64.jar;D:\soft\Java\JDK1.7\jre\lib\ext\dnsns.jar;D:\soft\Java\JDK1.7\jre\lib\ext\jaccess.jar;D:\soft\Java\JDK1.7\jre\lib\ext\localedata.jar;D:\soft\Java\JDK1.7\jre\lib\ext\sunec.jar;D:\soft\Java\JDK1.7\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\JDK1.7\jre\lib\ext\sunmscapi.jar;D:\soft\Java\JDK1.7\jre\lib\ext\zipfs.jar;D:\soft\Java\JDK1.7\jre\lib\javaws.jar;D:\soft\Java\JDK1.7\jre\lib\jce.jar;D:\soft\Java\JDK1.7\jre\lib\jfr.jar;D:\soft\Java\JDK1.7\jre\lib\jfxrt.jar;D:\soft\Java\JDK1.7\jre\lib\jsse.jar;D:\soft\Java\JDK1.7\jre\lib\management-agent.jar;D:\soft\Java\JDK1.7\jre\lib\plugin.jar;D:\soft\Java\JDK1.7\jre\lib\resources.jar;D:\soft\Java\JDK1.7\jre\lib\rt.jar;D:\myworkspace\02localdemo\T03TomLoader\target\classes com.zc.test.ReentrantLockVsSynchronizedTest<br>-------------------------------------<br>// 线程数1,每个线程循环 100000 次<br>22840888(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000<br>26080245(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000<br>8243398(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000<br>-------------------------------------<br>// 线程数2,每个线程循环 100000 次<br>5484065(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000<br>5490809(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000<br>11825254(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000<br>-------------------------------------<br>// 线程数4,每个线程循环 100000 次<br>11477771(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000<br>15624982(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000<br>29080910(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000<br>-------------------------------------<br>// 线程数6,每个线程循环 100000 次<br>15473673(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000<br>32343434(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000<br>35170504(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000<br>-------------------------------------<br>// 线程数8,每个线程循环 100000 次<br>18780768(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000<br>19249649(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000<br>51532676(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000<br>-------------------------------------<br>// 线程数10,每个线程循环 100000 次<br>22142404(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000<br>28116170(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000<br>46799583(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000<br>-------------------------------------<br>// 线程数50,每个线程循环 100000 次<br>154866338(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000<br>164094417(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000<br>190372008(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000<br>-------------------------------------<br>// 线程数100,每个线程循环 100000 次<br>320082668(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000<br>399773154(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000<br>669262375(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000<br>-------------------------------------<br>// 线程数200,每个线程循环 100000 次<br>557621097(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000<br>650982745(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000<br>1287148955(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000<br>-------------------------------------<br>// 线程数500,每个线程循环 100000 次<br>1319411162(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000<br>1496867673(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000<br>2878890484(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000<br>-------------------------------------<br>// 线程数500,每个线程循环 10000 次<br>134509730(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000<br>170702742(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000<br>250160106(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000<br>-------------------------------------<br>// 线程数500,每个线程循环 1000 次<br>27824108(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000<br>40523787(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000<br>40963344(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000<br>-------------------------------------<br>// 线程数500,每个线程循环 100 次<br>24163960(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100<br>24996452(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100<br>35085760(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100<br>-------------------------------------<br>// 线程数500,每个线程循环 10 次<br>40494463(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10<br>51199269(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10<br>52468973(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10<br>-------------------------------------<br>// 线程数500,每个线程循环 1 次<br>44156664(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1<br>46565583(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1<br>144862068(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1<br>-------------------------------------<br>// 线程数1000,每个线程循环 1 次<br>111706963(elapse), testReentrantLockUnfair: result=93656500, threadCount=1000, loopCount=1<br>156553607(elapse), testAtomicInteger: result=93656500, threadCount=1000, loopCount=1<br>182228017(elapse), testSynchronized: result=93656500, threadCount=1000, loopCount=1<br>-------------------------------------<br>// 线程数1500,每个线程循环 1 次<br>176627244(elapse), testSynchronized: result=93658000, threadCount=1500, loopCount=1<br>202298721(elapse), testAtomicInteger: result=93658000, threadCount=1500, loopCount=1<br>69693840(elapse), testReentrantLockUnfair: result=93658000, threadCount=1500, loopCount=1<br>-------------------------------------<br><br>Process finished with exit code 0<br><br></code></pre></td></tr></table></figure>
<p>从数据上看,正常情况下还是 synchronized 稍微快一点的.有比 ReentrantLock(非公平)慢的时候,最多也就慢一倍这样.</p>
<h2 id="JDK1-6"><a href="#JDK1-6" class="headerlink" title="JDK1.6"></a>JDK1.6</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">D:\soft\Java\jdk1.6.0_45\bin\java.exe &quot;-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=61893:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin&quot; -Dfile.encoding=UTF-8 -classpath D:\soft\Java\jdk1.6.0_45\jre\lib\charsets.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\deploy.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\dnsns.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\localedata.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\sunmscapi.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\javaws.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\jce.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\jsse.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\management-agent.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\plugin.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\resources.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\rt.jar;D:\myworkspace\02localdemo\T03TomLoader\target\classes com.zc.test.ReentrantLockVsSynchronizedTest<br>-------------------------------------<br>// 线程数1,每个线程循环 100000 次<br>4767108(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000<br>5231298(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000<br>11269281(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000<br>-------------------------------------<br>// 线程数2,每个线程循环 100000 次<br>2265820(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000<br>5181154(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000<br>9388771(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000<br>-------------------------------------<br>// 线程数4,每个线程循环 100000 次<br>10028901(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000<br>28028493(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000<br>9190838(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000<br>-------------------------------------<br>// 线程数6,每个线程循环 100000 次<br>15937569(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000<br>17108746(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000<br>21985524(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000<br>-------------------------------------<br>// 线程数8,每个线程循环 100000 次<br>20291510(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000<br>20961550(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000<br>36662479(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000<br>-------------------------------------<br>// 线程数10,每个线程循环 100000 次<br>21589658(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000<br>23756071(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000<br>27164038(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000<br>-------------------------------------<br>// 线程数50,每个线程循环 100000 次<br>126872447(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000<br>149717147(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000<br>296352695(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000<br>-------------------------------------<br>// 线程数100,每个线程循环 100000 次<br>240213603(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000<br>246687627(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000<br>324364767(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000<br>-------------------------------------<br>// 线程数200,每个线程循环 100000 次<br>522835023(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000<br>749915548(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000<br>1348022018(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000<br>-------------------------------------<br>// 线程数500,每个线程循环 100000 次<br>1252515069(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000<br>1796960863(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000<br>3533896401(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000<br>-------------------------------------<br>-------------------------------------<br>// 线程数500,每个线程循环 10000 次<br>120825372(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000<br>173094359(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000<br>327061935(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000<br>-------------------------------------<br>// 线程数500,每个线程循环 1000 次<br>25813109(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000<br>26570239(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000<br>28246953(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000<br>-------------------------------------<br>// 线程数500,每个线程循环 100 次<br>25892576(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100<br>26211908(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100<br>32821991(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100<br>-------------------------------------<br>// 线程数500,每个线程循环 10 次<br>24638120(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10<br>28614668(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10<br>28632262(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10<br>-------------------------------------<br>// 线程数500,每个线程循环 1 次<br>28014418(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1<br>30410727(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1<br>37675310(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1<br>-------------------------------------<br>// 线程数1000,每个线程循环 1 次<br>49730166(elapse), testAtomicInteger: result=93656500, threadCount=1000, loopCount=1<br>54536567(elapse), testSynchronized: result=93656500, threadCount=1000, loopCount=1<br>55911835(elapse), testReentrantLockUnfair: result=93656500, threadCount=1000, loopCount=1<br>-------------------------------------<br>// 线程数1500,每个线程循环 1 次<br>77436982(elapse), testReentrantLockUnfair: result=93658000, threadCount=1500, loopCount=1<br>80245284(elapse), testSynchronized: result=93658000, threadCount=1500, loopCount=1<br>82252179(elapse), testAtomicInteger: result=93658000, threadCount=1500, loopCount=1<br>-------------------------------------<br><br>Process finished with exit code 0<br><br></code></pre></td></tr></table></figure>

<p>从上面数据看,线程数大于等于6后 ReentrantLockUnfair 快于 synchronized,线程数小的时候 synchronized 快一些.</p>
<h2 id="JDK11"><a href="#JDK11" class="headerlink" title="JDK11"></a>JDK11</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><code class="hljs plaintext">D:\soft\Java\jdk-11.0.4\bin\java.exe -Didea.launcher.port=62514 &quot;-Didea.launcher.bin.path=D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin&quot; -Dfile.encoding=UTF-8 -classpath &quot;D:\myworkspace\02localdemo\T03TomLoader\target\classes;D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar&quot; com.intellij.rt.execution.application.AppMainV2 com.zc.test.ReentrantLockVsSynchronizedTest<br>-------------------------------------<br>// 线程数1,每个线程循环 100000 次<br>4207617(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000<br>7003311(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000<br>12754512(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000<br>-------------------------------------<br>// 线程数2,每个线程循环 100000 次<br>4266557(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000<br>6544986(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000<br>10791310(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000<br>-------------------------------------<br>// 线程数4,每个线程循环 100000 次<br>7923773(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000<br>9049499(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000<br>10100450(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000<br>-------------------------------------<br>// 线程数6,每个线程循环 100000 次<br>13618380(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000<br>14245901(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000<br>15905607(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000<br>-------------------------------------<br>// 线程数8,每个线程循环 100000 次<br>14097525(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000<br>14484007(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000<br>20932226(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000<br>-------------------------------------<br>// 线程数10,每个线程循环 100000 次<br>9905157(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000<br>28208539(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000<br>40982698(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000<br>-------------------------------------<br>// 线程数50,每个线程循环 100000 次<br>67569652(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000<br>119220208(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000<br>127837187(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000<br>-------------------------------------<br>// 线程数100,每个线程循环 100000 次<br>155161331(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000<br>214199043(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000<br>271890809(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000<br>-------------------------------------<br>// 线程数200,每个线程循环 100000 次<br>346384305(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000<br>465400423(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000<br>658154372(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000<br>-------------------------------------<br>// 线程数500,每个线程循环 100000 次<br>930565965(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000<br>1305456443(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000<br>1745141403(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000<br>-------------------------------------<br>-------------------------------------<br>// 线程数500,每个线程循环 10000 次<br>88273743(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000<br>132994002(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000<br>165907776(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000<br>-------------------------------------<br>// 线程数500,每个线程循环 1000 次<br>32739299(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000<br>34829766(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000<br>37466527(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000<br>-------------------------------------<br>// 线程数500,每个线程循环 100 次<br>34564096(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100<br>35123587(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100<br>40711163(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100<br>-------------------------------------<br>// 线程数500,每个线程循环 10 次<br>33697882(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10<br>34600750(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10<br>36510584(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10<br>-------------------------------------<br>// 线程数500,每个线程循环 1 次<br>35484557(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1<br>35623551(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1<br>36133778(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1<br>-------------------------------------<br>// 线程数1000,每个线程循环 1 次<br>66762085(elapse), testAtomicInteger: result=93656500, threadCount=1000, loopCount=1<br>67941473(elapse), testReentrantLockUnfair: result=93656500, threadCount=1000, loopCount=1<br>74041623(elapse), testSynchronized: result=93656500, threadCount=1000, loopCount=1<br>-------------------------------------<br>// 线程数1500,每个线程循环 1 次<br>98764781(elapse), testReentrantLockUnfair: result=93658000, threadCount=1500, loopCount=1<br>100622126(elapse), testSynchronized: result=93658000, threadCount=1500, loopCount=1<br>102833697(elapse), testAtomicInteger: result=93658000, threadCount=1500, loopCount=1<br>-------------------------------------<br><br>Process finished with exit code 0<br><br></code></pre></td></tr></table></figure>
<p>线程数少的时候还是 synchronized 快,线程数多的时候二者基本持平.</p>
<h2 id="测试使用的代码"><a href="#测试使用的代码" class="headerlink" title="测试使用的代码"></a>测试使用的代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.zc.concurrent.issue;<br><br><span class="hljs-keyword">import</span> java.util.concurrent.CountDownLatch;<br><span class="hljs-keyword">import</span> java.util.concurrent.atomic.AtomicInteger;<br><span class="hljs-comment">// import java.util.concurrent.atomic.LongAdder;</span><br><span class="hljs-keyword">import</span> java.util.concurrent.atomic.LongAdder;<br><span class="hljs-keyword">import</span> java.util.concurrent.locks.ReentrantLock;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ReentrantLockVsSynchronizedTest</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">AtomicInteger</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AtomicInteger</span>(<span class="hljs-number">0</span>);<br><br>    <span class="hljs-comment">// JDK8 才有</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">LongAdder</span> <span class="hljs-variable">b</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">LongAdder</span>();<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-variable">c</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-variable">d</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> <span class="hljs-variable">e</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">ReentrantLock</span> <span class="hljs-variable">fairLock</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReentrantLock</span>(<span class="hljs-literal">true</span>);<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">ReentrantLock</span> <span class="hljs-variable">unfairLock</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ReentrantLock</span>();<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">1</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">2</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">4</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">6</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">8</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">10</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">50</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">100</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">200</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">500</span>, <span class="hljs-number">100000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">500</span>, <span class="hljs-number">10000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">500</span>, <span class="hljs-number">1000</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">500</span>, <span class="hljs-number">100</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">500</span>, <span class="hljs-number">10</span>);<br><br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">500</span>, <span class="hljs-number">1</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">1000</span>, <span class="hljs-number">1</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>        testAll(<span class="hljs-number">1500</span>, <span class="hljs-number">1</span>);<br><br>        System.out.println(<span class="hljs-string">&quot;-------------------------------------&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testAll</span><span class="hljs-params">(<span class="hljs-type">int</span> threadCount, <span class="hljs-type">int</span> loopCount)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        testAtomicInteger(threadCount, loopCount);<br><br>        testLongAdder(threadCount, loopCount);<br><br>        testSynchronized(threadCount, loopCount);<br><br>        testReentrantLockUnfair(threadCount, loopCount);<br><br>        testReentrantLockFair(threadCount, loopCount);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testAtomicInteger</span><span class="hljs-params">(<span class="hljs-type">int</span> threadCount, <span class="hljs-type">int</span> loopCount)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">long</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> System.nanoTime();<br><br>        <span class="hljs-type">CountDownLatch</span> <span class="hljs-variable">countDownLatch</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CountDownLatch</span>(threadCount);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; threadCount; i++) &#123;<br>            <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(() -&gt; &#123;<br>                <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; loopCount; j++) &#123;<br>                    a.incrementAndGet();<br>                &#125;<br>                countDownLatch.countDown();<br>            &#125;).start();<br>        &#125;<br><br>        countDownLatch.await();<br><br>        System.out.println((System.nanoTime() - start) + <span class="hljs-string">&quot;(elapse), &quot;</span> + <span class="hljs-string">&quot;testAtomicInteger: result=&quot;</span> + a.get() + <span class="hljs-string">&quot;, threadCount=&quot;</span> + threadCount + <span class="hljs-string">&quot;, loopCount=&quot;</span> + loopCount);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testLongAdder</span><span class="hljs-params">(<span class="hljs-type">int</span> threadCount, <span class="hljs-type">int</span> loopCount)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">long</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> System.nanoTime();<br><br>        <span class="hljs-type">CountDownLatch</span> <span class="hljs-variable">countDownLatch</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CountDownLatch</span>(threadCount);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; threadCount; i++) &#123;<br>            <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(() -&gt; &#123;<br>                <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; loopCount; j++) &#123;<br>                    b.increment();<br>                &#125;<br>                countDownLatch.countDown();<br>            &#125;).start();<br>        &#125;<br><br>        countDownLatch.await();<br><br>        System.out.println((System.nanoTime() - start) + <span class="hljs-string">&quot;(elapse), &quot;</span> + <span class="hljs-string">&quot;testLongAdder: result=&quot;</span> + b.sum() + <span class="hljs-string">&quot;, threadCount=&quot;</span> + threadCount + <span class="hljs-string">&quot;, loopCount=&quot;</span> + loopCount);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testReentrantLockFair</span><span class="hljs-params">(<span class="hljs-type">int</span> threadCount, <span class="hljs-type">int</span> loopCount)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">long</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> System.nanoTime();<br><br>        <span class="hljs-type">CountDownLatch</span> <span class="hljs-variable">countDownLatch</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CountDownLatch</span>(threadCount);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; threadCount; i++) &#123;<br>            <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(() -&gt; &#123;<br>                <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; loopCount; j++) &#123;<br>                    fairLock.lock();<br>                    c++;<br>                    fairLock.unlock();<br>                &#125;<br>                countDownLatch.countDown();<br>            &#125;).start();<br>        &#125;<br><br>        countDownLatch.await();<br><br>        System.out.println((System.nanoTime() - start) + <span class="hljs-string">&quot;(elapse), &quot;</span> + <span class="hljs-string">&quot;testReentrantLockFair: result=&quot;</span> + c + <span class="hljs-string">&quot;, threadCount=&quot;</span> + threadCount + <span class="hljs-string">&quot;, loopCount=&quot;</span> + loopCount);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testReentrantLockUnfair</span><span class="hljs-params">(<span class="hljs-type">int</span> threadCount, <span class="hljs-type">int</span> loopCount)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">long</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> System.nanoTime();<br><br>        <span class="hljs-type">CountDownLatch</span> <span class="hljs-variable">countDownLatch</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CountDownLatch</span>(threadCount);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; threadCount; i++) &#123;<br>            <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(() -&gt; &#123;<br>                <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; loopCount; j++) &#123;<br>                    unfairLock.lock();<br>                    d++;<br>                    unfairLock.unlock();<br>                &#125;<br>                countDownLatch.countDown();<br>            &#125;).start();<br>        &#125;<br><br>        countDownLatch.await();<br><br>        System.out.println((System.nanoTime() - start) + <span class="hljs-string">&quot;(elapse), &quot;</span> + <span class="hljs-string">&quot;testReentrantLockUnfair: result=&quot;</span> + d + <span class="hljs-string">&quot;, threadCount=&quot;</span> + threadCount + <span class="hljs-string">&quot;, loopCount=&quot;</span> + loopCount);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">testSynchronized</span><span class="hljs-params">(<span class="hljs-type">int</span> threadCount, <span class="hljs-type">int</span> loopCount)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        <span class="hljs-type">long</span> <span class="hljs-variable">start</span> <span class="hljs-operator">=</span> System.nanoTime();<br><br>        <span class="hljs-type">CountDownLatch</span> <span class="hljs-variable">countDownLatch</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CountDownLatch</span>(threadCount);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; threadCount; i++) &#123;<br>            <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(() -&gt; &#123;<br>                <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; loopCount; j++) &#123;<br>                    <span class="hljs-keyword">synchronized</span> (ReentrantLockVsSynchronizedTest.class) &#123;<br>                        e++;<br>                    &#125;<br>                &#125;<br>                countDownLatch.countDown();<br>            &#125;).start();<br>        &#125;<br><br>        countDownLatch.await();<br><br>        System.out.println((System.nanoTime() - start) + <span class="hljs-string">&quot;(elapse), &quot;</span> + <span class="hljs-string">&quot;testSynchronized: result=&quot;</span> + e + <span class="hljs-string">&quot;, threadCount=&quot;</span> + threadCount + <span class="hljs-string">&quot;, loopCount=&quot;</span> + loopCount);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10/dist/style.css"><script src="https://fastly.jsdelivr.net/npm/d3@7"></script><script src="https://fastly.jsdelivr.net/npm/markmap-view@0.18.10"></script><script src="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10"></script>
<link rel="stylesheet" href="/css/markmap.css">

<script src="/js/markmap.js"></script>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io">flyingzc</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io/articles/concurrent/concurrent10-sync-vs-lock.html">https://flyingzc.github.io/articles/concurrent/concurrent10-sync-vs-lock.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://flyingzc.github.io" target="_blank">flyingzc's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Concurrent/">Concurrent</a></div><div class="post-share"><div class="social-share" data-image="/images/zc-icon.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">flyingzc</div><div class="author-info-description">不积跬步无以至千里<br/>不积小流无以成江海</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FlyingZC"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/FlyingZC" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">FlyingZC's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#synchronized-%E4%B8%8E-Lock-%E6%80%A7%E8%83%BD%E6%AF%94%E8%BE%83"><span class="toc-number">1.</span> <span class="toc-text">synchronized 与 Lock 性能比较</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK1-8"><span class="toc-number">1.1.</span> <span class="toc-text">JDK1.8</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK1-7"><span class="toc-number">1.2.</span> <span class="toc-text">JDK1.7</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK1-6"><span class="toc-number">1.3.</span> <span class="toc-text">JDK1.6</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK11"><span class="toc-number">1.4.</span> <span class="toc-text">JDK11</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E4%BD%BF%E7%94%A8%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="toc-number">1.5.</span> <span class="toc-text">测试使用的代码</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite03-transaction-begin.html" title="Apache Ignite 开启事务执行流程">Apache Ignite 开启事务执行流程</a><time datetime="2025-10-10T16:00:00.000Z" title="发表于 2025-10-11 00:00:00">2025-10-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite02-transaction.html" title="Apache Ignite 分布式事务原理">Apache Ignite 分布式事务原理</a><time datetime="2025-09-17T16:00:00.000Z" title="发表于 2025-09-18 00:00:00">2025-09-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite01-build.html" title="Apache Ignite IDEA 环境搭建">Apache Ignite IDEA 环境搭建</a><time datetime="2025-09-10T16:00:00.000Z" title="发表于 2025-09-11 00:00:00">2025-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/druid/druid01-resultset-leak.html" title="ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析">ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/jmeter/jmeter01-shardingsphere.html" title="Jmeter 压测 ShardingSphere">Jmeter 压测 ShardingSphere</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2025 By flyingzc</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>